Skip to content

use LazyBufferCache instead of FixedSizeDiffCache #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025

Conversation

jClugstor
Copy link
Member

@jClugstor jClugstor commented Apr 30, 2025

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

This makes EnzymeVJP use LazyBufferCache instead of FixedSizeDiffCache to avoid problems with reinterpret and Enzyme.

@ChrisRackauckas
Copy link
Member

This shouldn't be needed now?

@jClugstor
Copy link
Member Author

Yeah, I'll close it

@jClugstor jClugstor closed this Apr 30, 2025
@jClugstor jClugstor reopened this May 1, 2025
@jClugstor
Copy link
Member Author

@ChrisRackauckas this fixes the Core2 tests.

@ChrisRackauckas
Copy link
Member

But there's a lot of other broken tests it creates?

@jClugstor jClugstor force-pushed the enzyme_adjoint_lbc branch from 4988fe1 to a55f1de Compare May 9, 2025 14:33
@jClugstor
Copy link
Member Author

I don't see any here that aren't also broken on master?

@ChrisRackauckas
Copy link
Member

What's the performance change from this?

@ChrisRackauckas
Copy link
Member

And which tests does this fix?

@jClugstor
Copy link
Member Author

This just fixes the Core 2 tests on Julia 1.11.

As for performance I'll set a benchmark to see.

@jClugstor
Copy link
Member Author

I ran this:

using Zygote, SciMLSensitivity
using OrdinaryDiffEq, ForwardDiff, Test
using BenchmarkTools

p = rand(3)

function dudt(u, p, t)
    u .* p
end


function loss(p, sensealg)
    prob = ODEProblem(dudt, [3.0, 2.0, 1.0], (0.0, 10.0), p)
    sol = solve(prob, ImplicitEuler(), dt=0.01, saveat=0.1, sensealg=sensealg,
        abstol=1e-5, reltol=1e-5)
    sum(abs2, Array(sol))
end

@btime Zygote.gradient(
    p -> loss(p, QuadratureAdjoint(autojacvec=EnzymeVJP())), p)


function lv(du, u, p, t)
    du[1] = p[1] * u[1] - p[2] * u[1] * u[2]
    du[2] = -3 * u[2] + u[1] * u[2]
end

function loss_lv(p,sensealg)
    prob_lv = ODEProblem(lv, [1.0, 2.0], (0.0, 10.0), p)
    sol = solve(prob_lv, ImplicitEuler(), dt=0.01, saveat=0.1, sensealg=sensealg,
        abstol=1e-5, reltol=1e-5)
    sum(abs2, Array(sol))
end

@btime Zygote.gradient(
    p -> loss_lv(p, QuadratureAdjoint(autojacvec=EnzymeVJP())), [1.0, 2.0])


function vanderpol(du, u, p, t)
    du[1] = u[2]
    du[2] = p[1]*((1-u[1]^2)*u[2] - u[1])
end

function loss_vp(p, sensealg)
    prob_vp = ODEProblem(vanderpol, [1.0, 2.0], (0.0, 10.0), p)
    sol = solve(prob_vp, Rodas5(), dt=0.01, saveat=0.1, sensealg=sensealg,
        abstol=1e-5, reltol=1e-5)
    sum(abs2, Array(sol))
end

@btime Zygote.gradient(
    p -> loss_vp(p, QuadratureAdjoint(autojacvec=EnzymeVJP())), [20.0])

With FixedSizeDiffCache and LazyBufferCache on Julia 1.10.

These are the results I got from @btime
image

@ChrisRackauckas ChrisRackauckas merged commit 9dfbfdc into SciML:master May 12, 2025
25 of 42 checks passed
@ChrisRackauckas
Copy link
Member

Open an issue about this performance regression. It's minor enough to not be a major worry, but we should track it because if Enzyme fixes their part then we should prefer DiffCache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants